#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <cassert>

#include "../comm/log.hpp"
#include "../comm/until.hpp"

// mod 模块主要用于获取数据交互
// 获取题目列表、详细题目等

namespace NS_MODEL
{
    using namespace std;
    using namespace NS_LOG;
    using namespace NS_UNTIL;

    // 描述问题类
    // 1 求最大值 easy 1 30000
    struct Question
    {
        string number;
        string title;
        string star;
        int cpu_limit;
        int mem_limit;
        string desc;
        string header;
        string tail;
    };

    const std::string question_list = "./questions/question_list";
    const std::string question_path = "./questions/";
    const std::string Seq = " ";
    class mod
    {
    private:
        std::unordered_map<string, Question> questions;

    public:
        mod()
        {
            assert(LoadQuestionList(question_list));
        }
        ~mod() {}

        //
        bool LoadQuestionList(const std::string &qest_list)
        {
            ifstream in(qest_list);
            if (!in.is_open())
            {
                LOG(Error) << "录入题库失败" << endl;
                return false;
            }
            string line;
            while (getline(in, line))
            {
                vector<string> tokens;

                StringUntil::SplitString(line, &tokens, Seq);
                if (tokens.size() != 5)
                {
                    LOG(Warning) << "录入部分题目失败" << endl;
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = std::stoi(tokens[3]);
                q.mem_limit = std::stoi(tokens[4]);

                std::string path = question_path + q.number + "/";
                if (!FileUntil::ReadFile(path + "desc.txt", &q.desc, true))
                {
                    LOG(Warning) << path << "desc.txt 读取失败" << endl;
                }

                if (!FileUntil::ReadFile(path + "header.cpp", &q.header, true))
                {
                    LOG(Warning) << path << "header.cpp 读取失败" << endl;
                }
                if (!FileUntil::ReadFile(path + "tail.cpp", &q.tail, true))
                {
                    LOG(Warning) << path << "tail.cpp 读取失败" << endl;
                }
                questions.insert({q.number,q});
            }
            LOG(Info)<<"录入题目成功!"<<endl;
            in.close();
            return true;
        }

        //加载所有的题目
        bool GetAllQustions(vector<Question> *out)
        {
            if(questions.size()==0)
            {
                LOG(Error)<<"加载题目失败"<<endl;
                return false;
            }
            for(auto &iter:questions)
            {
                out->push_back(iter.second);
            }
            return true;
        }

        bool GetQuestion(const std::string &number, Question *q)
        {
            const auto &iter=questions.find(number);
            if(iter==questions.end())
            {
                LOG(Warning)<<"没有此题目"<<endl;
                return false;
            }
            (*q)=iter->second;
            return true;
        }
    };

} // namespace NS_MODEL
